package com.dc.adaptor.transport.addressconvertor;

import com.dc.adaptor.transport.TransportMeta;
import com.dc.adaptor.transport.ftp.FtpMeta;


public class FTPAddressConvertor implements AddressConvertor<TransportMeta> {

	@Override
	public String getAddress(TransportMeta transMeta) {
		if(transMeta == null){
			throw new RuntimeException("involid parameter FtpMeta : "+" null ");
		}
		FtpMeta meta = (FtpMeta)transMeta;
		StringBuffer address = new StringBuffer("");
		address.append("ftp").append(AddressConvertor.SEPARATOR).append(
				processNullToString(meta.getServerName())).append(AddressConvertor.SEPARATOR).append(//1
				processNullToString(meta.getPort())).append(AddressConvertor.SEPARATOR).append(//2
				processNullToString(meta.getFtpDirectory())).append(AddressConvertor.SEPARATOR).append(//3
				processNullToString(meta.getUserName())).append(AddressConvertor.SEPARATOR).append(//4
				processNullToString(meta.getPassword())).append(AddressConvertor.SEPARATOR).append(//5
				processNullToString(meta.getWildcard())).append(AddressConvertor.SEPARATOR).append(
				processNullToString(meta.getControlEncoding())).append(AddressConvertor.SEPARATOR).append(//7
				meta.getTimeout()).append(AddressConvertor.SEPARATOR).append(
				meta.isRemove()).append(AddressConvertor.SEPARATOR).append(
				meta.isBinaryMode()).append(AddressConvertor.SEPARATOR).append(
				meta.isActiveConnection()).append(AddressConvertor.SEPARATOR).append(
				meta.getFileEncoding()).append(AddressConvertor.SEPARATOR).append(
				processNullToString(meta.getBackupDirectory())).append(AddressConvertor.SEPARATOR).append(
				meta.isBackup());
		return address.toString();
	}

	@Override
	public TransportMeta getMeta(final String address) {
		final String [] addresses = address.split(AddressConvertor.SEPARATOR);
		if(addresses.length != 15){
			throw new RuntimeException("invalid parameter for the charaters: '"+address+"'");
		}
		FtpMeta meta = new FtpMeta(){

			@Override
			public String getControlEncoding() {
				return processBlankString(addresses[7]);
			}

			@Override
			public String getFtpDirectory() {
				return processBlankString(addresses[3]);
			}

			@Override
			public String getPassword() {
				return processBlankString(addresses[5]);
			}

			@Override
			public String getPort() {
				return processBlankString(addresses[2]);
			}

			@Override
			public String getServerName() {
				return processBlankString(addresses[1]);
			}

			@Override
			public int getTimeout() {
				return Integer.parseInt(addresses[8]);
			}

			@Override
			public String getUserName() {
				return processBlankString(addresses[4]);
			}

			@Override
			public String getWildcard() {
				return processBlankString(addresses[6]);
			}

			@Override
			public boolean isActiveConnection() {
				if("Y".equals(addresses[11])){
					return true;
				}else{
					return false;
				}
			}

			@Override
			public boolean isBinaryMode() {
				if("Y".equals(addresses[10])){
					return true;
				}else{
					return false;
				}
			}

			@Override
			public boolean isRemove() {
				if("Y".equals(addresses[9])){
					return true;
				}else{
					return false;
				}
			}

			@Override
			public String getFileEncoding() {
				return processBlankString(addresses[12]);
			}

			@Override
			public String getBackupDirectory() {
				// TODO Auto-generated method stub
				return processBlankString(addresses[13]);
			}

			@Override
			public boolean isBackup() {
				if("Y".equals(addresses[14])){
					return true;
				}else{
					return false;
				}
			}};
			return meta;
	}
	
	private String processNullToString(String orinString){
		if(orinString == null){
			return "";
		}
		return orinString;
	}

	private String processBlankString(String orinString){
		if(orinString.length()==0){
			return null;
		}
		return orinString;
	}
}
